import Joi from '@hapi/joi';
import { paginateRule } from '../common';

export const indexRule = {
  query: {
    name: Joi.string().trim(),
    category_id: Joi.string(),
    ...paginateRule,
  },
};

export const createRule = {
  body: {
    name: Joi.string()
      .trim()
      .required(),
    nick: Joi.string()
      .trim()
      .allow(''),
    category_id: Joi.number()
      .required(),
    online: Joi.number(),
    main_pic: Joi.string()
      .trim()
      .required(),
    brief: Joi.string()
      .trim()
      .allow(''),
    desc: Joi.string()
      .trim(),
    banner: Joi.array()
      .items(Joi.object({
        id: Joi.number(),
        url: Joi.string(),
        name: Joi.string()
      }))
      .required(),
    stock: Joi.number().allow(null),
    cost_price: Joi.number().allow(null),
    market_price: Joi.number().required(),
    price: Joi.number().required(),
  },
};


export const updateRule = {
  params: {
    id: Joi.string(),
  },
  body: createRule.body
}


export const detailRule = {
  query: {
    user_id: Joi.string().allow(''),
    id: Joi.string(),
  },
};

export const galleryRule = {
  params: {
    id: Joi.string(),
  },
};

export const destroyRule = {
  params: {
    id: Joi.string(),
  },
};

export const attrvalRule = {
  params: {
    id: Joi.string(),
  },
  body: {
    goods_attr: Joi.array()
      .items(Joi.object({
        composeIds: Joi.string(),
        composeNames: Joi.string(),
        price: Joi.number(),
        stock: Joi.number(),
      }))
      .required(),
    attr_val: Joi.array()
      .items(Joi.object({
        attr_id: Joi.number(),
        attr_name: Joi.string(),
        vals: Joi.array()
          .items(Joi.object({
            attr_key_id: Joi.number(),
            checked: Joi.boolean(),
            id: Joi.number(),
            name: Joi.string(),
            shop_id: Joi.number().allow(null)
          }))
      }))
      .required(),
  }
}